<style>
    #img{
        max-width: 100vw;
    }
</style>

<div class="layuimini-main">
    <img id="img">
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" lay-reqtext="资源标题不能为空" placeholder="请输入标题" value="" class="layui-input" id="title-input">
                <tip>填写班级的名称，如 19-3数据科学与大数据技术。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">资源简介</label>
            <div class="layui-input-block">
                <textarea name="describe" autocomplete="off" placeholder="请输入资源简介" class="layui-input" id="describe-input"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">资源标签</label>
            <div class="layui-input-block">
                <select name="city" lay-verify="" lay-search id="tag-select">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否公开</label>
            <div class="layui-input-block">
                <input type="checkbox" name="isPublic" lay-skin="switch" lay-text="是|否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拍摄日期</label>
            <div class="layui-input-block">
                <input type="text" name="date" id="date" lay-verify="date" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拍摄地点</label>
            <div class="layui-input-block">
                <input type="text" name="location" lay-verify="location" autocomplete="off" placeholder="请输入拍摄地点"
                       id="location"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">班级人数</label>
            <div class="layui-input-block">
                <input type="text" name="totalnumber" lay-verify="totalnumber" autocomplete="off" placeholder="请输入班级人数"
                       class="layui-input"
                id="totalnumber">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">参与人数</label>
            <div class="layui-input-block">
                <input type="text" name="realnumber" lay-verify="realnumber" autocomplete="off" placeholder="请输入参与人数"
                       class="layui-input"
                id="realnumber">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <fieldset>
                <legend>人员表</legend>
                <table id="oTable" class="layui-table">
                <!-- <thead>
                <tr>
                <th>第一列标题</th>
                <th>第二列标题</th>
                </tr>
                </thead> -->
                <tbody>
                </tbody>
                </table>
            </fieldset>
    <input id="num" type="text"  placeholder="请输入下一排的人数">
    <input type="button" onclick="addRow()" value="添加">
</div>
<script>
    function addRow(){
            var num = jq1111('#num').val()
            console.log(num)
            var oTable = document.getElementById("oTable");
            var tBodies = oTable.tBodies;
            var tbody = tBodies[0];
            var tr = tbody.insertRow(tbody.rows.length);
            for (var i = 0; i <parseInt(num) ; i++) {
            var td_1 = tr.insertCell(i);
            td_1.innerHTML = "<div contenteditable='true'></div>";
            }
            /*
            var td_1 = tr.insertCell(0);
            td_1.innerHTML = "<div contenteditable='true'>第1列</div>";
            var td_2 = tr.insertCell(1);
            td_2.innerHTML = "<div contenteditable='true'>第2列</div>";
            var td_2 = tr.insertCell(2);
            td_2.innerHTML = "<div contenteditable='true'>第3列</div>";

             */
            }
    var peopleList; //用来接收ajax返回的数组

    //这个方法是把数组中的元素渲染成表格
    function renderPeoleList(){
        var oTable = document.getElementById('oTable')
        var tBodies = oTable.tBodies
        var tbody = tBodies[0]
        for (var i = 0; i <peopleList.length ; i++) {
            var tr = tbody.insertRow(i)
            for (var j = 0; j <peopleList[i].length ; j++) {
                var td_1 = tr.insertCell(j);
                var fornt = '<div contenteditable="true">'
                var behind = '</div>'
                td_1.innerHTML = fornt+peopleList[i][j]+behind
                console.log(' i am td elm'+peopleList[i][j])
            }
        }

    }

    // 这个方法是把把表格中的元素保存为数组 返回数组
    function getAllPeople(){
        peopleList = []
        jq1111('#oTable tr').each(function (i) {
            var curRow= []
            jq1111(this).children('td').each(function (j) {
                curRow.push(jq1111(this).text())
                console.log(jq1111(this).text())
            })
            peopleList.push(curRow)
        })
        return peopleList
    }

    //ajax得到数据后渲染整个form的函数
    //暂时没用到 渲染各个参数还是混合在一块
    function RenderForm(message){
        console.log(message)
        jq1111("#title-input").val(message.data.resourceTitle)
          //  jq1111("#describe-input").val(message.data.resourceDescription)
        jq1111("#describe-input").val(message.data.resourceDescription['description'])
        peopleList = JSON.parse(message.data.resourceDescription)['peopleList']
        renderPeoleList()

    }
    layui.use(['form', 'table','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$,
            laydate = layui.laydate;

        //初始化表单，要加上，不然刷新部分组件可能会不加载
        form.render();
        //日期
        laydate.render({
            elem: '#date'
        });

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var isPub = true;
            if (data.field.isPublic === 'off') {
                isPub = false;
            }
            console.log(jq1111("#upload-file"))
            var postdata = {}
            var description={
                "description": jq1111('#describe-input').val(),
                "peopleList": getAllPeople(),
                "totalnumber": data.field.totalnumber,
                "realnumber": data.field.realnumber
                    }
            /*
            formData.append('isPublic',isPub)
            formData.append('id',resId)
            formData.append('title',data.field.title)
            formData.append('description',JSON.stringify(description))*/
            postdata['isPublic']=isPub
            postdata['id']=resId
            postdata['title']=jq1111("#title-input").val()
            postdata['description']=JSON.stringify(description)
            jq1111.ajax({
                type: "POST",
                crossDomain: true,
                url: BasePath+"api/Resource/Update",
                processData: false,
                contentType : "application/json",
                dataType: "json",
                xhrFields: {
                    withCredentials: true
                },
                data:JSON.stringify(postdata),
                success: function (message) {
                    console.log(message);
                    var index=layer.alert(message.msg, {
                        title: '操作结果'
                    },function (){
                        // 关闭弹出层
                        layer.close(index);
                        layer.close(parentIndex);
                    })
                },
                error: function (message) {
                    console.log(message.responseJSON);
                    layer.alert(message.responseJSON.msg, {
                        title: '操作结果'
                    })
                },
            });

            return false;
        });

        //获取所有TAG
        var tagData;
        jq1111.ajax({
            type: "GET",
            crossDomain: true,
            url: BasePath+"api/ResourceTag/",
            processData: false,
            contentType: false,
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            success: function (message) {
                console.log(message);
                tagData=message.data;
                tagData.forEach(function (tag) {
                    jq1111("#tag-select").append(
                        '<option value="'+tag.id+'">'+tag.name+'</option>'
                    )
                });
                form.render();
            },
            error: function (message) {
                console.log(message.responseJSON);
            },
        });

        //获取资源信息
        var resId=localStorage.getItem('selectedResId');
        // var resType=localStorage.getItem('selectedResType');
        jq1111.ajax({
            type: "GET",
            crossDomain: true,
            url: BasePath+"api/Resource/"+resId+"/Details",
            processData: false,
            contentType: false,
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            success: function (message) {
                jq1111('#img').attr('src',BasePath+"api/Resource/"+resId)
                console.log(message)
                jq1111("#title-input").val(message.data.resourceTitle)
          //      jq1111("#describe-input").val(message.data.resourceDescription)
                jq1111("#describe-input").val(JSON.parse(message.data.resourceDescription)['description'])
                peopleList = JSON.parse(message.data.resourceDescription)['peopleList']
                jq1111("#totalnumber").val(JSON.parse(message.data.resourceDescription)['totalnumber'])
                jq1111("#realnumber").val(JSON.parse(message.data.resourceDescription)['realnumber'])
                jq1111("#location").val(message.data.location)
                renderPeoleList()
                form.render();
            },
            error: function (message) {
                console.log(message.responseJSON);
            },
        });



    });

</script>